package demo.filter;

import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants;
import org.springframework.stereotype.Component;

import com.netflix.zuul.ZuulFilter;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Component
public class AccessFilter extends ZuulFilter {

	@Override
	public boolean shouldFilter() {
		return true;
	}

	@Override
	public Object run() {
		
		// get the access token from security context
//		RequestContext ctx = RequestContext.getCurrentContext();
//		Object accessToken = SecurityContextUtil.getAccessToken();
//		log.debug("***Pre AccessTokenFilter  username:{}, accessToken:{}", accessToken);
//		if (accessToken == null) {
//			ctx.setResponseStatusCode(401);
//			ctx.setResponseBody("Access Token Not Found, Access Defined!");
//		} else {
//			// add the access token to Authorization header
//			ctx.addZuulRequestHeader("Authorization", "Bearer " + accessToken);
//		}
				
//		Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
//		if (authentication == null || authentication instanceof AnonymousAuthenticationToken) {
//			RequestContext ctx = RequestContext.getCurrentContext();
//			ctx.setResponseStatusCode(401);
//			ctx.setResponseBody("Access Defined!");			
//		}

		return null;
	}

	@Override
	public String filterType() {
		return FilterConstants.PRE_TYPE;
	}

	@Override
	public int filterOrder() {
		return 0;
	}
}
